package app.ui.panels.event {
	import com.bit101.components.ScrollPane;
	import com.bit101.components.VBox;
	import bme.data.datatypes.BRC_Occurrence;
	import org.osflash.signals.Signal;

	import app.ui.panels.IBrowserPanel;

	import bme.data.datatypes.BRC_Event;

	import com.bit101.components.Label;
	import com.bit101.components.Panel;
	import com.bit101.components.TextArea;
	import com.greensock.TweenMax;

	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.text.TextField;

	/**
	 * @author Rendall
	 */
	public class EventUI extends Panel implements IBrowserPanel {
		private var titleLabel : Label;
		private var description : TextArea;

		private static var eventuiPool : Array;

		public static var target : DisplayObjectContainer;
		private var campLabel : Label;
		private var _event : BRC_Event;
		private var timesbox : VBox;
		private var timeScroll : ScrollPane;

		public function EventUI(parent : DisplayObjectContainer = null, xpos : Number = 0, ypos : Number = 0) {
			super(parent, xpos, ypos);
			setUI();
			
			if (!eventuiPool) eventuiPool = new Array();
			eventuiPool.push(this);
		}

		private function setUI() : void {
			this.setSize(540, 300);
			this.color = 0xCCCCCC;
			titleLabel = new Label(this, 5, 5, "Event Title");
			titleLabel.autoSize = true;
			
			campLabel = new Label(this, 0, titleLabel.y, "Hosted By Camp");
			//campLabel.autoSize = true;
			
			var fieldY : Number = titleLabel.x + titleLabel.height + 5;
			
			description = new TextArea(this, 5, fieldY);
			description.setSize(this.width - 10, 100);
			
			var timeY : uint = description.y + description.height + 5;
			
			//timeScroll = new ScrollPane(this,5,timeY);
			//timeScroll.height = 290 - timeY;
			//timeScroll.
			//timesbox = new VBox(timeScroll.content, 5, 5);			timesbox = new VBox(this, 5, timeY);
			timesbox.spacing = 1;

		}
		
		private function inscribe(event : BRC_Event) : void {
			trace("\n EventUI.inscribe( event:" + event + ")");
			_event = event;
			titleLabel.text = event.title;
			//titleLabel.setSize(titleLabel.width, titleLabel.height);
			
			if (event.hosted_by_camp) campLabel.text = event.hosted_by_camp.name;
			else campLabel.text = "";
			
			description.text = event.description.replace("\r\n", "\n");
			
			while(timesbox.numChildren) timesbox.removeChildAt(0);
			var twidth : int = 0;
			if (event.occurrence_set) for (var i:int = 0; i < event.occurrence_set.length; i++) {
				
				var occ : BRC_Occurrence = event.occurrence_set[i] as BRC_Occurrence;
				var timeLabel : Label = new Label(null, 0, 0, occ.getFormattedFull(event.all_day));
				timesbox.addChildAt(timeLabel, i);
				twidth = Math.max(timeLabel.width,twidth);
			        
			}
			//timeScroll.width = twidth + 30;
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event : Event) : void {
			this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			//var tf : TextField = titleLabel.textField;
			//titleLabel.width = tf.x * 2 + tf.textWidth;
			//titleLabel.draw();
			
			var cf : TextField = campLabel.textField;
			campLabel.width = cf.x * 2 + cf.textWidth;
			campLabel.draw();
			campLabel.x = this.width - campLabel.width - 5;
		}

		
		
		public function show(option : *=  null) : void {
			this.visible = true;
			if (option=="prev") this.x = -10 - this.width; 
			else this.x = 560;
			TweenMax.to(this, 0.5, {x:5});
			//this.visible = true;
		}

		public function hide(option : *=  null) : void {
			var toX : Number;//this.visible = false;
			
			if (option == "prev") toX = 560;
			else toX = -10 - this.width;
			TweenMax.to(this, 0.5, {x:toX, onComplete:onHideComplete});
		}

		private function onHideComplete() : void {
			this.visible = false;
			//this.x = -10 - this.width;	
		}

		public static function getPanel(event : BRC_Event) : EventUI {
			var eventui : EventUI;
			for each (var eUI : EventUI in eventuiPool) {
				if (!eUI.visible) {
					eventui = eUI;
					break;	
				}
			}
			
			if (!eventui) eventui = new EventUI(target);
			eventui.inscribe(event);
			
			return eventui;
		}
		
		public function get stateChangeSignal() : Signal {
			// TODO: Auto-generated method stub
			throw new Error("implement this");
			return null;
		}
		
		public function get event() : BRC_Event {
			return _event;
		}
	}
}
